package com.doublefish.doublefishrobot.ui.home.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;

import com.doublefish.doublefishrobot.R;

public class ProgressView extends RelativeLayout {

    private Paint bgPaint; // 背景圆环画笔
    private Paint progressPaint;   // 进度圆弧画笔
    private int progress = 0;      // 当前进度值 (0-100)
    private int strokeWidth = 20;  // 圆环宽度
    private int centerX, centerY;  // 圆心坐标
    private int radius;            // 圆环半径
    

    public ProgressView(Context context) {
        this(context, null);
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public ProgressView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    // 设置进度值并刷新
    public void setProgress(int progress) {
        this.progress = progress;
        invalidate(); // 触发重绘
    }

    private void init() {
        setBackgroundColor(Color.TRANSPARENT);

        float scale = getScale();
        strokeWidth = (int)(10 * scale);

        // 初始化背景画笔（灰色）
        bgPaint = new Paint();
        bgPaint.setColor(getResources().getColor(R.color.colorPrimary));
        bgPaint.setStyle(Paint.Style.STROKE);
        bgPaint.setStrokeWidth(strokeWidth);
        bgPaint.setAntiAlias(true);

        // 初始化进度画笔（蓝色）
        progressPaint = new Paint();
        progressPaint.setColor(Color.BLUE);
        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setStrokeWidth(strokeWidth);
        progressPaint.setAntiAlias(true);
        progressPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        // 计算圆心和半径（考虑 padding）
        centerX = w / 2;
        centerY = h / 2;
        radius = (Math.min(w, h) - getPaddingLeft() - getPaddingRight()) / 2 - strokeWidth / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 绘制背景圆环
        canvas.drawCircle(centerX, centerY, radius, bgPaint);
        // 绘制进度圆弧（根据进度计算角度）
        float sweepAngle = (float) (360 * progress / 100);
        canvas.drawArc(
                centerX - radius,
                centerY - radius,
                centerX + radius,
                centerY + radius,
                -90, // 起始角度（-90度表示从12点方向开始）
                sweepAngle,
                false,
                progressPaint
        );
    }

    private float getScale() {
        float scale = getResources().getDisplayMetrics().density;
        return scale;
    }
}
